//////////////////////////////////////////////////////////////////////////
#include "UI/Document.hpp"
#include "UI/Context.hpp"


using namespace UI;


Document::Document()
:	mpContext( nullptr )
,	mpDocument( nullptr )
{
}


Document::Document( Context& ctx, const std::string& name )
:	mName( name )
,	mpContext( &ctx )
{
	mpDocument	=	ctx.LoadDocument( name.c_str() );
}


Document::~Document()
{
	if( mpDocument )
	{
		mpDocument->RemoveReference();
	}
}


bool Document::Load( Context& ctx, const std::string& name )
{
	mName		=	name;
	mpContext	=	&ctx;
	mpDocument	=	ctx.LoadDocument( name.c_str() );
	return mpDocument!=nullptr;
}


void Document::Show()
{
	if( mpDocument )
		mpDocument->Show();
}


void Document::Unload()
{
	if( mpDocument )
	{
		mpDocument->GetContext()->UnloadDocument( mpDocument );
		mpDocument->RemoveReference();
		mpDocument	=	nullptr;
	}
}


void Document::Reload()
{
	if( mpContext )
	{
		Unload();
		mpContext->ClearCache();
		Load( *mpContext, mName );
		mpDocument->Show();
	}
}


Rocket::Core::Element* Document::GetElementById( const std::string& name )
{
	if( mpDocument )
		return mpDocument->GetElementById( name.c_str() );
	return nullptr;
}


void Document::UpdatePosition()
{
	mpDocument->UpdatePosition();
}
